package com.aegis.gateway.service;

import com.aegis.gateway.constants.FilterChainEnum;
import com.aegis.manage.api.dto.SysAppDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ServerWebExchange;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
@Slf4j
@Service
public class FilterManageService {

    private static final String FILTER_FLAG_IS_INTERRUPT = "filter_flag_is_interrupt";

    private static final String FILTER_EXECUTION_CHAIN = "filter_execution_chain";

    private static final String FILTER_APP_INFO = "filter_app_info";


//    /**
//     * 判断是否中断当前过滤器处理
//     *
//     * @param exchange
//     * @return
//     */
//    public static boolean isInterrupt(ServerWebExchange exchange) {
//        Object o = exchange.getAttributes().get(FILTER_FLAG_IS_INTERRUPT);
//        if (o == null) {
//            return false;
//        }
//        return (Boolean) o;
//    }
//
//
//    /**
//     * 中断过滤器执行链
//     *
//     * @param exchange
//     * @param reason   中断原因
//     */
//    public static void interrupt(ServerWebExchange exchange, String reason) {
//        log.info("filter interrupt, reason: {}", reason);
//        cacheAttribute(FILTER_FLAG_IS_INTERRUPT, true);
//    }
//
//    /**
//     * 中断过滤器执行链: 白名单
//     *
//     * @param exchange
//     */
//    public static void interruptByWhiteList(ServerWebExchange exchange) {
//        interrupt(exchange, "white list");
//    }
//
//    /**
//     * 中断过滤器执行链：当前过滤器处理请求
//     *
//     * @param exchange
//     */
//    public static void interruptByTakeRequest(ServerWebExchange exchange) {
//        interrupt(exchange, "current filter take this request");
//    }

    public void setAppInfo(ServerWebExchange exchange, SysAppDTO sysAppDTO) {
        setAttribute(FILTER_APP_INFO, sysAppDTO, exchange);
    }

    public SysAppDTO getAppInfo(ServerWebExchange exchange) {
        return (SysAppDTO) exchange.getAttributes().get(FILTER_APP_INFO);
    }


    /**
     * @param key
     * @param value
     * @param exchange
     */
    public void setAttribute(String key, Object value, ServerWebExchange exchange) {
        exchange.getAttributes().put(key, value);
    }

    /**
     * 设置下一个处理请求的过滤器，只能填入比当前过滤器优先级还低的过滤器
     *
     * @param exchange
     * @param filterChainEnum
     */
    public void setNextFilter(ServerWebExchange exchange, FilterChainEnum filterChainEnum) {
        log.info("===> next filter: {}", filterChainEnum.getFilterClass().getName());
        setAttribute(FILTER_EXECUTION_CHAIN, filterChainEnum, exchange);
    }

    /**
     * @param exchange
     * @return
     */
    public FilterChainEnum getNextFilter(ServerWebExchange exchange) {
        return (FilterChainEnum) exchange.getAttributes().get(FILTER_EXECUTION_CHAIN);
    }

    /**
     * 过滤器的业务结束，中断后续的过滤器业务执行（不包括后置过滤器）
     * @param exchange
     */
    public void filterChainEnd(ServerWebExchange exchange) {
        log.info("===> business filter end");
        setNextFilter(exchange, FilterChainEnum.AFTER_FILTER);
    }


}
